<?php
    $xtpl_search    = new XTemplate("view/search/search.htm");
    
    $total      = 0;
    $supplier   = "";
    
    $sql_s    = "SELECT * FROM supplier";
    $re_s     = mysql_query($sql_s);
    while($rows = mysql_fetch_array($re_s)){
        $supplier   .= '<option value="' . $rows['name'] . '">' . $rows['name'] . '</option>';
    }
    $xtpl_search    -> assign('supplier', $supplier);
    
    $keyword        = $_POST['keyword'];
   	$keyword		= str_replace(" ", "%", $keyword);
   	$keyword 		= "LIKE '%" . $keyword . "%'";

    if(isset($_POST['submit'])){
        if($_POST['keyword']!=NULL){
            if(($_POST['bySupplier'] == 0) && ($_POST['byCost'] == 0)){
                $sql    = "SELECT * FROM product WHERE milkname $keyword";
                $result = mysql_query($sql);
                while($row = mysql_fetch_array($result)){
                    $xtpl_search    -> assign('id1', $row['id_p']);
                    $xtpl_search    -> assign('image', $row['image']);
                    $xtpl_search    -> assign('id2', $row['id_p']);
                    $xtpl_search    -> assign('milkname', $row['milkname']);
                    $xtpl_search    -> assign('cost', $row['cost']);
                    $xtpl_search    -> assign('id3', $row['id_p']);
                    $xtpl_search    -> insert_loop('SEARCH.TABLES', array('SEARCH'=>$row));
                    $total++;
                }
            }
            else if(($_POST['bySupplier'] != 0) && ($_POST['byCost'] == 0)){
                $bySupplier = $_POST['bySupplier'];
                $condition  = "provider LIKE '%" . $bySupplier . "%'"; 
                $sql       = "SELECT * FROM product WHERE milkname $keyword AND $condition";
                $result    = mysql_query($sql);
                while($row = mysql_fetch_array($result)){
                    $xtpl_search    -> assign('id1', $row['id_p']);
                    $xtpl_search    -> assign('image', $row['image']);
                    $xtpl_search    -> assign('id2', $row['id_p']);
                    $xtpl_search    -> assign('milkname', $row['milkname']);
                    $xtpl_search    -> assign('cost', $row['cost']);
                    $xtpl_search    -> assign('id3', $row['id_p']);
                    $xtpl_search    -> insert_loop('SEARCH.TABLES', array('SEARCH'=>$row));
                    $total++;                           
                }                
            }
            else if(($_POST['bySupplier'] == 0) && ($_POST['byCost'] != 0)){
                $byCost     = $_POST['byCost'];
                switch($byCost){
                    case    '1'     : $condition = "cost < 10"; break;
                    case    '2'     : $condition = "cost >= 10 AND cost <= 100"; break;
                    case    '3'     : $condition = "cost >= 100 AND cost <= 300"; break;
                    case    '4'     : $condition = "cost > 300"; break;
                }
                
                $sql    = "SELECT * FROM product WHERE milkname $keyword AND $condition";
                $result = mysql_query($sql);
                while($row = mysql_fetch_array($result)){
                    $xtpl_search    -> assign('id1', $row['id_p']);
                    $xtpl_search    -> assign('image', $row['image']);
                    $xtpl_search    -> assign('id2', $row['id_p']);
                    $xtpl_search    -> assign('milkname', $row['milkname']);
                    $xtpl_search    -> assign('cost', $row['cost']);
                    $xtpl_search    -> assign('id3', $row['id_p']);
                    $xtpl_search    -> insert_loop('SEARCH.TABLES', array('SEARCH'=>$row));
                    $total++;
                }                
            }
            else if(($_POST['bySupplier'] != 0) && ($_POST['byCost'] != 0)){
                $bySupplier = $_POST['bySupplier'];
                $condition  = "provider LIKE '%" . $bySupplier . "%'";
                $byCost     = $_POST['byCost'];
                switch($byCost){
                    case    '1'     : $condition .= " AND cost < 10"; break;
                    case    '2'     : $condition .= " AND cost >= 10 AND cost <= 100"; break;
                    case    '3'     : $condition .= " AND cost >= 100 AND cost <= 300"; break;
                    case    '4'     : $condition .= " AND cost > 300"; break;
                }
                $sql    = "SELECT * FROM product WHERE milkname $keyword AND $condition";
                $result = mysql_query($sql);
                while($row = mysql_fetch_array($result)){
                    $xtpl_search    -> assign('id1', $row['id_p']);
                    $xtpl_search    -> assign('image', $row['image']);
                    $xtpl_search    -> assign('id2', $row['id_p']);
                    $xtpl_search    -> assign('milkname', $row['milkname']);
                    $xtpl_search    -> assign('cost', $row['cost']);
                    $xtpl_search    -> assign('id3', $row['id_p']);
                    $xtpl_search    -> insert_loop('SEARCH.TABLES', array('SEARCH'=>$row));
                    $total++;
                }                 
            }

            $xtpl_search    -> assign('total', $total);
            $xtpl_search    -> assign('keyword', $_POST['keyword']);
            $xtpl_search    -> parse("SEARCH.TOTAL");
        }
        else{
            $error = "Bạn chưa điền từ khóa";
            $xtpl_search  -> assign('error', $error);
        }
    }

    $xtpl_search    -> parse("SEARCH");
    $mid            = $xtpl_search -> text("SEARCH");
?>